package 数据结构和算法入门计划8月开始;

import java.util.HashMap;
import java.util.Map;
// https://leetcode-cn.com/problems/first-unique-character-in-a-string/
public class 字符串中的第一个唯一字符3870820 {
    class Solution {
        public int firstUniqChar(String s) {
            Map<Character, Integer> frequency = new HashMap<Character, Integer>();
            for (int i = 0; i < s.length(); ++i) {
                char ch = s.charAt(i);
                frequency.put(ch, frequency.getOrDefault(ch, 0) + 1);
            }
            for (int i = 0; i < s.length(); ++i) {
                if (frequency.get(s.charAt(i)) == 1) {
                    return i;
                }
            }
            return -1;
        }
    }

}
